import argparse

from client import Client
from enums import CourseType


def main(args=None):
    type_choices = [
        CourseType.FREE.value,
        CourseType.MINI.value,
        CourseType.PRACTICE.value,
    ]

    parser = argparse.ArgumentParser(prog="慕课网视频下载器", description="输入 账号、密码、课程ID、课程类型 进行下载")
    parser.add_argument("-u", "--username", dest="username", required=True, help="账号")
    parser.add_argument("-p", "--password", dest="password", required=True, help="密码")
    parser.add_argument("-c", "--course", dest="course", required=True, help="课程ID")
    parser.add_argument("-t", "--type", dest="type", choices=type_choices, type=int, help="课程类型: (1)免费课程, (2)微课, (3)实战课程")

    params = parser.parse_args(args)

    if not params.type:
        while True:
            print("选择课程类型: (1)免费课程, (2)微课, (3)实战课程")
            try:
                params.type = CourseType(int(input("请输入课程类型数字:")))
                break
            except ValueError:
                print("课程类型无效!请重新输入...")
                continue

    client = Client(params.username, params.password)
    client.login()
    client.download(params.course, params.type)


if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        pass
